/*
 * @Description:
 * @Version: 2.0
 * @Autor: zhangjx
 * @Date: 2023-06-02 14:30:03
 * @LastEditors: uphldjx 1373577124@qq.com
 * @LastEditTime: 2024-05-27 14:29:52
 */
import { ElNotification, ElMessage } from 'element-plus'
import apiMod from '@/apiMod/index'
import { useUserStore } from '@/stores/user'
import { useCommonStore } from '@/stores/common'

export const UserCommonHooks = (paramsObj, crud, titleTip = '请点击选择所属行政单位') => {
	const userStore = useUserStore()
	const { setGloablParams } = useCommonStore()
	let nowTreeData: any = {}

	const operateFormate = async (row) => {
		return new Promise((resolve, reject) => {
			const areaObj = getAreaObj(nowTreeData.parentPath, nowTreeData.regionGrade, nowTreeData.administrationId)
			if ((paramsObj.classifyId == 4 || paramsObj.classifyId == 5) && !nowTreeData.organizationId) {
				ElNotification({
					title: '提示',
					message: titleTip,
					type: 'warning',
				})
				return
			} else if (nowTreeData.administrationId || nowTreeData.organizationId || row.userId) {
				resolve({
					...row,
					...paramsObj,
					...areaObj,
					userType: nowTreeData.userType,
					administrationName: nowTreeData.administrationName,
					administrationOrganizationId: paramsObj.classifyId == 1 ? nowTreeData.administrationId : '',
					organizationId: paramsObj.classifyId == 4 ? nowTreeData.creditCode : nowTreeData.creditCode || nowTreeData.organizationId,
					organizationName: nowTreeData.organizationName || nowTreeData.administrationName,
				})
			} else {
				ElNotification({
					title: '提示',
					message: titleTip,
					type: 'warning',
				})
			}
		})
	}
	const handleRefresh = () => {
		crud.value!.refreshChange({
			...paramsObj,
		})
	}

	const getAreaObj = (parentPath, regionGrade, administrationId) => {
		const areaObj = {
			authProvince: '',
			authCity: '',
			authArea: '',
			authStreet: '',
			authCommunity: '',
		}
		if (parentPath) {
			const areaData = parentPath.split(',')
			for (let index = 0; index < regionGrade; index++) {
				let key = Object.keys(areaObj)[index]
				areaObj[key] = areaData[index] || administrationId
			}
		}
		return areaObj
	}
	const handleNodeClick = (data) => {
		const areaObj = getAreaObj(data.parentPath, data.regionGrade, data.administrationId)
		paramsObj = {
			...paramsObj,
			...areaObj,
		}
		// if (data.parentId == -1) {
		// 	paramsObj.province = data.administrationId
		// }
		if (paramsObj.classifyId == 3) {
			paramsObj.administrationOrganizationId = data.administrationId
		}
		if (paramsObj.classifyId == 4) {
			// 服务组织用户
			paramsObj.organizationId = data.creditCode
			paramsObj.userType = data.userType
		}
		if (paramsObj.classifyId == 5) {
			paramsObj.organizationId = data.creditCode || data.organizationId
		}
		setGloablParams(paramsObj)
		crud.value!.refreshChange({
			...paramsObj,
		})
		nowTreeData = data
	}
	const saveFormate = async (form) => {
		return new Promise((resolve, reject) => {
			resolve(form)
		})
	}
	const onChange = (row, type) => {
		// 判断有没有权限
		const all_auth = userStore.permissions
		if (!all_auth['sys_user_edit']) {
			return
		}
		const value = row['lockFlag']
		apiMod
			.sys_user_edit({
				...row,
				password: '',
				lockFlag: value == 0 ? 9 : 0,
			})
			.then((res) => {
				handleNodeClick(nowTreeData)
				ElMessage.success('操作成功!')
			})
	}

	onUnmounted(() => {
		setGloablParams({})
	})
	return {
		onChange,
		handleRefresh,
		saveFormate,
		operateFormate,
		handleNodeClick,
	}
}
